package com.hongxun.design.ui.statisticsmap.brokenline;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;

import com.hongxun.design.R;
import com.hongxun.design.mytools.MPoint;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("DrawAllocation")
public class BrokenLineView extends View {
    // 默认边距
    private int Margin = 60;
    // 原点坐标
    private int Xpoint;
    private int Ypoint;

    // X,Y轴的单位长度
    private int Xscale = 20;
    private int Yscale = 25;

    private int ValueY = 1;//Y轴上面的显示文字刻度
    int Y_code = 1;//Y轴上面的显示文字刻度实际代表长度
    // X,Y轴上面的显示文字
    private List<String> Xlabel;
     // 标题文本
    private String Title;

    int color = Color.WHITE;
    Context context;
    private int minDataY = 0;
    private int maxDataY = 0;
    // 曲线数据
    private List<List<Integer>> data = new ArrayList<>();

    private int[] color_all = new int[]{R.color.project_blue, R.color.login_yellow,R.color.charity};

    public BrokenLineView(Context context) {
        super(context);
        this.context = context;
    }


    public BrokenLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public BrokenLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    public int getcolor(int i){
        if (i<data.size()){
            if (i < color_all.length) {
               return color_all[i] ;
            } else {
                return color_all[i % color_all.length];
            }
        }
        return 0;
    }

    // 初始化数据值
    public void init() {
        int screenW = this.getWidth();
        int screenH = this.getHeight();
        int sizeX = Xlabel.size();
        int sizeY = getData();//Y轴等分

        Xpoint = this.Margin;//X轴初始坐标
        Ypoint = screenH - this.Margin;//Y轴初始坐标

        Xscale = (screenW - 2 * this.Margin) / (sizeX + 1);//确定X轴坐标等分
        Yscale = (screenH - 2 * this.Margin) / (sizeY + 1);//确定Y轴坐标等分
        if (sizeY >= 1) {
            ValueY = (maxDataY) / (sizeY);//确定Y轴坐标等分所对应的值
        }
       if (ValueY==0)ValueY=1;

           Y_code = Yscale / ValueY;
    }

    private int getData() {
        int size = 1;
        boolean flag = true;
        for (List<Integer> i : data) {
            if (size < i.size()) {
                size = i.size();
            }
            for (int j = 0; j < i.size(); j++) {

                if (maxDataY < i.get(j)) {
                    maxDataY = i.get(j);
                }
                if (flag) {
                    flag = false;
                    minDataY = maxDataY;
                }
                if (minDataY == 0 || minDataY > i.get(j)) {
                    minDataY = i.get(j);
                }
            }
        }

        MyLog.L(MString.SUCCESSED + " minDataY=" + minDataY + " maxDataY=" + maxDataY + " size=" + size);
        return size;
    }

    public int getMargin() {
        return Margin;
    }

    public void setMargin(int margin) {
        Margin = margin;
    }

    @Override
    public void setBackgroundColor(int color) {
        super.setBackgroundColor(color);
        this.color = color;
    }

    public void setData( List<String> Xlabel,List<Integer>... data){
        this.Xlabel=Xlabel;
        this.data.clear();
        for (int i=0;i<data.length;i++){
            this.data.add(data[i]);
        }
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(color);
        //坐标轴画笔
        Paint p1 = new Paint();
        p1.setStyle(Paint.Style.STROKE);
        p1.setAntiAlias(true);//抗锯齿
        p1.setColor(Color.GRAY);
        p1.setStrokeWidth(3);//画笔宽度
        if (Xlabel!=null&&Xlabel.size()>0){
            init();// 初始数据
            this.drawXLine(canvas, p1);// 画x轴
            this.drawYLine(canvas, p1);// 画y轴
            this.drawPoint(canvas);// 画原点
            this.drawTable(canvas);// 画表格
            this.drawData(canvas);// 画数据
        }

    }


    // 画横纵轴
    private void drawXLine(Canvas canvas, Paint p) {
        canvas.drawLine(Xpoint, Ypoint, Xpoint, this.Margin, p);//Y轴

    }

    private void drawYLine(Canvas canvas, Paint p) {
        canvas.drawLine(Xpoint, Ypoint, this.getWidth() - this.Margin, Ypoint, p);
    }
    private void drawPoint(Canvas canvas){
        Paint paint_line = new Paint();
        paint_line.setStyle(Paint.Style.STROKE);
        paint_line.setColor(Color.GRAY);
       
        paint_line.setTextSize(DensityUtil.dip2px(context, 10));
        canvas.drawText("0", Xpoint - this.Margin / 2, Ypoint+ this.Margin / 2,  paint_line);//原点
    }

    // 画表格
    private void drawTable(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.GRAY);

        Paint paint_line = new Paint();
        paint_line.setStyle(Paint.Style.STROKE);
        paint_line.setColor(Color.GRAY);
        paint_line.setStrokeWidth(3);

        /**虚线绘制**/
        Path path = new Path();
        PathEffect effects = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);
        paint.setPathEffect(effects);
        // 纵向线
        for (int i = 1; (i + 1) * Xscale <= (this.getWidth() - this.Margin); i++) {
            int startX = Xpoint + i * Xscale;
            int startY = Ypoint;
            paint.setTextSize(DensityUtil.dip2px(context, 10));
            canvas.drawText(this.Xlabel.get(i - 1) + "", startX - this.Margin / 2, startY + this.Margin * 2 / 3, paint);//X轴刻度
            canvas.drawLine(startX, startY, startX, startY + this.Margin / 4, paint_line);//X轴刻度

        }
        // 横向线
        for (int i = 1; (Ypoint - i * Yscale) >= this.Margin; i++) {
            int startX = Xpoint;
            int startY = Ypoint - i * Yscale;

            int stopX = Xpoint + (this.Xlabel.size() + 1) * Xscale;
            path.moveTo(startX, startY);
            path.lineTo(stopX, startY);

            paint.setColor(Color.DKGRAY);
            canvas.drawPath(path, paint);

            paint.setColor(Color.GRAY);
            paint.setTextSize(DensityUtil.dip2px(context, 10));

            canvas.drawText((ValueY * i) + "", this.Margin/8, startY + this.Margin / 4, paint);//Y轴刻度
            canvas.drawLine(startX, startY, startX - this.Margin / 4, startY, paint_line);//Y轴刻度

        }
    }

    // 画数据
    private void drawData(Canvas canvas) {
        /**线条绘制**/
        Paint p = new Paint();
        p.setAntiAlias(true);//抗锯齿
        p.setStrokeWidth(12);
        p.setColor(Color.BLUE);

//        Paint text_p = new Paint();
//        text_p.setColor(Color.GRAY);
//        text_p.setTextSize(DensityUtil.dip2px(context, 10));

        Paint text_v = new Paint();
        text_v.setColor(Color.GRAY);
        text_v.setTextSize(DensityUtil.dip2px(context, 10));


        List<MPoint> list = new ArrayList<>();
        int radiu = 18;
        int colors = 0;
        for (List<Integer> y : data) {
            if (colors < color_all.length) {
                p.setColor(setcolor(color_all[colors]));
            } else {
                p.setColor(setcolor(color_all[colors % color_all.length]));
            }

            for (int i = 1; (i + 1) * Xscale <= (this.getWidth() - this.Margin); i++) {
                int startX = Xpoint + i * Xscale;
                canvas.drawCircle(startX, Ypoint - y.get(i - 1)  * Y_code, radiu, p);//圆点
                if (i != 1) {
                    canvas.drawLine(startX, Ypoint -  y.get(i - 1) * Y_code, Xpoint + (i - 1) * Xscale, Ypoint -  y.get(i - 2) * Y_code, p);//连接线
                }
//            canvas.drawText(this.Xlabel[i], startX - this.Margin / 4,
//                    this.getHeight() - this.Margin / 4, text_p);
                MPoint point = new MPoint();
                point.set(startX - radiu, Ypoint -  y.get(i - 1) * Y_code + radiu / 2);
                point.setValue( y.get(i - 1) + "");
                list.add(point);
            }
            colors++;
        }


        /**绘制原点中数字**/
        for (MPoint po : list) {
            String value = po.getValue();
            if (value.equals("0")){

            }else if (value.length() > 2) {
                canvas.drawText(value, po.x, po.y-DensityUtil.dip2px(context, 10), text_v);//
            } else {
                canvas.drawText(value, po.x + radiu / (value.length() + 1), po.y-DensityUtil.dip2px(context, 10), text_v);//
            }
        }

    }

    private int setcolor(int color){
        return context.getResources().getColor(color);
    }

}
